/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package camada.bancodedados.daoimplemente.material;

import camada.bancodedados.conexao.Conexao;
import eloma.classe.material.TipoMaterial;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author icaro.klinger
 */
public class TipoMaterialDAOImpl {

    public TipoMaterial pesquisarMaterialPorIdentificador(int idTipoMaterial) throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            final String SQL_PESQUISA_USUARIO = "select * from tipoMaterial where idTipoMaterial = ?";
            stmt = (PreparedStatement) Conexao.getCon().prepareStatement(SQL_PESQUISA_USUARIO);
            stmt.setInt(1, idTipoMaterial);
            rs = stmt.executeQuery();
            return carregarResultadoSimples(rs);
        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    public List<TipoMaterial> pesquisarTodosTipoMaterial() throws Exception {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String SQL_STATEMENT = "Select * from TipoMaterial order by tipoMaterial";
        try {
            stmt = (PreparedStatement) Conexao.getCon().prepareStatement(SQL_STATEMENT);
            rs = stmt.executeQuery();
            return carregarMultiplosResultados(rs);
        } catch (SQLException sqle) {
            throw new Exception(sqle);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
        }
    }

    public List<TipoMaterial> carregarMultiplosResultados(ResultSet rs) throws SQLException {
        List<TipoMaterial> resultList = new ArrayList<TipoMaterial>();
        while (rs.next()) {
            TipoMaterial dto = new TipoMaterial();
            carregarVO(dto, rs);
            resultList.add(dto);
        }
        return resultList;
    }

    private TipoMaterial carregarResultadoSimples(ResultSet rs) throws SQLException {
        if (rs.next()) {
            TipoMaterial dto = new TipoMaterial();
            carregarVO(dto, rs);
            return dto;
        } else {
            return null;
        }
    }

    private void carregarVO(TipoMaterial dto, ResultSet rs) throws SQLException {
        dto.setIdTipoMaterial(rs.getInt("idTipoMaterial"));
        dto.setTipoMaterial(rs.getString("tipoMaterial"));
    }
}
